﻿//namespace WebUI.Controls
//{
//    using MgExtensions.TrafficLights;
//    using MgExtensions.TrafficLights.DAL;
//    using System;
//    using System.Collections.Generic;
//    using System.Drawing;
//    using System.Globalization;
//    using System.Linq;
//    using System.Runtime.CompilerServices;
//    using System.Web;
//    using System.Web.UI;
//    using System.Web.UI.HtmlControls;
//    using System.Web.UI.WebControls;

//    public class AspIncidentControl : UserControl
//    {
//        protected bool _refreshMap;
//        private int _uid;
//        protected Button btnOk;
//        protected Button btnSetPropAsSelected;
//        protected DropDownList CmbDamageType;
//        protected DropDownList CmbFixType;
//        protected DropDownList CmbPriority;
//        protected DropDownList CmbPropType;
//        protected DropDownList CmbReporterType;
//        protected DropDownList CmbStatus;
//        protected HtmlInputHidden ContractorID;
//        public MgExtensions.TrafficLights.DAL.Incident CurrentIncident;
//        protected HtmlInputText DtDue;
//        protected HtmlInputText DtEnd;
//        protected TextBox DtStart;
//        protected Label JuncName;
//        protected Label JunctionId;
//        protected HtmlGenericControl lblContractor;
//        protected HtmlGenericControl lblContractorName;
//        protected ScriptManagerProxy ScriptManagerProxy1;
//        protected HiddenField SelectedProp;
//        protected HiddenField SelectedPropLayerType;
//        protected TextBox tbxCallCenterID;
//        protected HtmlTextArea txtComment;
//        protected HtmlTextArea txtDamageDescription;
//        protected HtmlTextArea txtIncidentFactor;
//        protected TextBox txtLoggedBy;
//        protected HtmlInputText txtPropUID;
//        protected TextBox txtReporterEmail;
//        protected TextBox txtReporterName;
//        protected TextBox txtReporterPhone;
//        protected UpdatePanel UpdateNew;

//        public event EventHandler IncidentClosed;

//        internal void Bind(bool reset)
//        {
//            int? CS$0$0000;
//            if (this.IncidentId.HasValue)
//            {
//                CS$0$0000 = this.IncidentId;
//                this.CurrentIncident = this.TlManager.GetIncedentById(CS$0$0000.Value);
//                this.CurrentJunction = this.CurrentIncident.Junction;
//                if ((this.CurrentIncident.Prop != null) && (CS$0$0000 = this.CurrentIncident.Prop.UID).HasValue)
//                {
//                    CS$0$0000 = this.CurrentIncident.Prop.UID;
//                    this.UID = CS$0$0000.Value;
//                }
//                this.CurrentProp = this.CurrentIncident.Prop;
//                this.PropTypeId = this.CurrentIncident.PropTypeID;
//            }
//            else
//            {
//                CS$0$0000 = this.CurrentJunctionId;
//                CS$0$0000 = null;
//                this.CurrentJunction = this.TlManager.GetJuncion(CS$0$0000.Value, CS$0$0000);
//                User user = this.TlManager.GetUser(HttpContext.Current.User.Identity.Name);
//                this.txtLoggedBy.Text = (user != null) ? user.Title : "אנונימי";
//            }
//            this.CurrentProp = this.TlManager.GetPropByUID(this.UID);
//            if (this.CurrentProp != null)
//            {
//                this.txtPropUID.Value = this.UID.ToString();
//                this.PropTypeId = new int?(this.CurrentProp.PropTypeID);
//            }
//            else if (this.UID == 0)
//            {
//                this.txtPropUID.Value = this.UID.ToString();
//            }
//            else
//            {
//                this.txtPropUID.Value = this.UID.ToString();
//                if (string.IsNullOrEmpty(this.SelectedPropLayerType.Value))
//                {
//                    this.PropTypeId = null;
//                }
//            }
//            this.BindControls();
//            if (reset)
//            {
//                this.ResetFields();
//            }
//        }

//        private void BindControls()
//        {
//            if (this.CurrentJunctionId.HasValue)
//            {
//                List<PropType> propTypeList = this.TlManager.GetPropTypes(this.CurrentJunctionId.Value).ToList<PropType>();
//                if (propTypeList.Count > 1)
//                {
//                    PropType <>g__initLocal1 = new PropType {
//                        Name = "-- בחר מרשימה --"
//                    };
//                    propTypeList.Insert(0, <>g__initLocal1);
//                }
//                this.CmbPropType.DataSource = propTypeList;
//                this.CmbPropType.DataTextField = "Name";
//                this.CmbPropType.DataValueField = "ID";
//                if (propTypeList.Count == 1)
//                {
//                    this.CmbPropType.DataBind();
//                    this.PropTypeId = new int?(this.CmbPropType.SelectedValue.To<int>());
//                }
//            }
//            this.Contract = this.TlManager.GetContract(this.CurrentJunctionId.Value, this.PropTypeId);
//            this.lblContractor.InnerText = (this.Contract != null) ? this.Contract.ContractCode : "ללא חוזה";
//            this.lblContractorName.InnerText = (this.Contract != null) ? this.Contract.Contractor.CompanyName : "אין קבלן מטפל";
//            this.btnOk.Enabled = this.Contract != null;
//            this.JuncName.Text = this.CurrentJunction.Name;
//            this.JunctionId.Text = this.CurrentJunctionId.Value.ToString();
//            List<MgExtensions.TrafficLights.DAL.Priority> priorityList = this.TlManager.GetPriorities();
//            this.CmbPriority.DataSource = priorityList;
//            this.CmbPriority.DataTextField = "Name";
//            this.CmbPriority.DataValueField = "ID";
//            this.CmbReporterType.DataSource = this.TlManager.GetReporters();
//            this.CmbReporterType.DataTextField = "Type";
//            this.CmbReporterType.DataValueField = "ID";
//            this.DataBind();
//            List<MgExtensions.TrafficLights.DAL.Status> statusList = this.TlManager.GetStatuses();
//            this.CmbStatus.DataSource = statusList;
//            this.CmbStatus.DataTextField = "Name";
//            this.CmbStatus.DataValueField = "ID";
//            this.CmbStatus.DataBind();
//            for (int i = 0; i < this.CmbStatus.Items.Count; i++)
//            {
//                ListItem item = this.CmbStatus.Items[i];
//                item.Attributes.Add("OptionGroup", statusList[i].Group);
//            }
//            if (this.PropTypeId > 0)
//            {
//                this.CmbPropType.SelectedValue = this.PropTypeId.ToString();
//            }
//            else
//            {
//                this.CmbPropType.SelectedIndex = 0;
//            }
//            List<Fix> fixList = this.TlManager.GetFixesForPropType(int.Parse(this.CmbPropType.SelectedValue));
//            Fix <>g__initLocal2 = new Fix {
//                Name = ""
//            };
//            fixList.Insert(0, <>g__initLocal2);
//            this.CmbFixType.DataSource = fixList;
//            this.CmbFixType.DataTextField = "Name";
//            this.CmbFixType.DataValueField = "ID";
//            this.CmbFixType.DataBind();
//            List<Damage> damages = this.TlManager.GetDamagesForPropType(int.Parse(this.CmbPropType.SelectedValue));
//            Damage <>g__initLocal3 = new Damage {
//                Name = "-- בחר מרשימה --"
//            };
//            damages.Insert(0, <>g__initLocal3);
//            this.CmbDamageType.DataSource = damages;
//            this.CmbDamageType.DataTextField = "Name";
//            this.CmbDamageType.DataValueField = "ID";
//            this.CmbDamageType.DataBind();
//        }

//        protected void btnOk_Click(object sender, EventArgs e)
//        {
//            if (this.CurrentJunctionId.HasValue)
//            {
//                DateTime dtStart;
//                DateTime dtDue;
//                DateTime dtEnd;
//                DateTime? CS$0$0002;
//                Fix selectedFix = this.GetSelectedFix();
//                int selectedPropTypeId = int.Parse(this.CmbPropType.SelectedValue);
//                int.TryParse(this.txtPropUID.Value, out this._uid);
//                int selectedDamageTypeId = int.Parse(this.CmbDamageType.SelectedValue);
//                if (selectedDamageTypeId <= 0)
//                {
//                    this.CmbDamageType.BorderColor = Color.Red;
//                    return;
//                }
//                int selectedPriorityId = int.Parse(this.CmbPriority.SelectedValue);
//                int selectedContractId = this.TlManager.GetContract(this.CurrentJunctionId.Value, new int?(this.PropTypeId.Value)).ContractorID;
//                int selectedStatusId = int.Parse(this.CmbStatus.SelectedValue);
//                int selectedReporterId = int.Parse(this.CmbReporterType.SelectedValue);
//                if (!(!string.IsNullOrEmpty(this.DtStart.Text) && DateTime.TryParse(this.DtStart.Text, new CultureInfo("he-IL"), DateTimeStyles.None, out dtStart)))
//                {
//                    dtStart = DateTime.MinValue;
//                }
//                if (!DateTime.TryParse(this.DtDue.Value, new CultureInfo("he-IL"), DateTimeStyles.None, out dtDue))
//                {
//                    dtDue = DateTime.MinValue;
//                }
//                if (!DateTime.TryParse(this.DtEnd.Value, new CultureInfo("he-IL"), DateTimeStyles.None, out dtEnd))
//                {
//                    dtEnd = DateTime.MinValue;
//                }
//                this.CurrentProp = this.GetPropOrCreate(this.CurrentJunctionId.Value, this.UID, selectedPropTypeId);
//                MgExtensions.TrafficLights.DAL.Incident incident = this.IncidentId.HasValue ? this.GetIncident(this.IncidentId.Value) : this.CreateIncident(this.CurrentProp, this.CurrentJunctionId.Value);
//                SetIncidentProperties(incident, selectedDamageTypeId, this.tbxCallCenterID.Text, this.txtDamageDescription.InnerText, selectedReporterId, this.txtReporterName.Text, this.txtReporterPhone.Text, this.txtReporterEmail.Text, selectedContractId, selectedPriorityId, selectedStatusId, this.txtIncidentFactor.InnerText, selectedFix, this.txtComment.InnerText);
//                incident.DtStart = (dtStart > DateTime.MinValue) ? new DateTime?(dtStart) : ((DateTime?) (CS$0$0002 = null));
//                incident.DtDue = (dtDue > DateTime.MinValue) ? new DateTime?(dtDue) : ((DateTime?) (CS$0$0002 = null));
//                incident.DtEnd = (dtEnd > DateTime.MinValue) ? new DateTime?(dtEnd) : null;
//                int? statusLevel = (incident.Status == null) ? this.TlManager.GetStatus(incident.StatusID).Level : incident.Status.Level;
//                if (!((statusLevel >= 0) || incident.DtEnd.HasValue))
//                {
//                    incident.DtEnd = new DateTime?(DateTime.Now);
//                }
//                if (!this.IncidentId.HasValue)
//                {
//                    incident = this.TlManager.CreateIncident(incident);
//                }
//                incident.LastUpdate = DateTime.Now;
//                this.TlManager.SaveChanges();
//                this.OnIncidendSaved();
//                base.Session["LastReporterID"] = incident.UserID;
//                if (statusLevel < 0)
//                {
//                    this.OnIncidentClosed();
//                }
//            }
//            string redirectAddr = base.Request.RawUrl.Split(new string[] { "&Method" }, StringSplitOptions.RemoveEmptyEntries)[0];
//            base.Response.Redirect(redirectAddr + (this._refreshMap ? "&refresh=1" : ""));
//        }

//        protected void btnSetPropAsSelected_Click(object sender, EventArgs e)
//        {
//            if (this.SelectedProp.Value != null)
//            {
//                this.IncidentId = null;
//                this.UID = int.Parse(this.SelectedProp.Value);
//                PropType pt = this.TlManager.GetPropTypeByCode(this.SelectedPropLayerType.Value);
//                if (pt != null)
//                {
//                    this.PropTypeId = new int?(pt.ID);
//                }
//            }
//            this.Bind(true);
//        }

//        protected void CmbPropType_SelectedIndexChanged(object sender, EventArgs e)
//        {
//            if (string.IsNullOrEmpty(this.DtStart.Text))
//            {
//                this.DtStart.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
//            }
//            this.PropTypeId = new int?(int.Parse(this.CmbPropType.SelectedValue));
//            this.IncidentId = null;
//            this.txtPropUID.Value = null;
//            this.SelectedProp.Value = string.Empty;
//            this.SelectedPropLayerType.Value = string.Empty;
//            this.Bind(true);
//        }

//        private MgExtensions.TrafficLights.DAL.Incident CreateIncident(Prop currentProp, int selectedJunctionId)
//        {
//            MgExtensions.TrafficLights.DAL.Incident incident = new MgExtensions.TrafficLights.DAL.Incident();
//            if (currentProp != null)
//            {
//                incident.Prop = currentProp;
//            }
//            User user = this.TlManager.GetUser(HttpContext.Current.User.Identity.Name);
//            incident.User = user;
//            incident.PropTypeID = this.PropTypeId;
//            incident.Junction = this.TlManager.GetJuncion(selectedJunctionId, this.PropTypeId);
//            return this.TlManager.CreateIncident(incident);
//        }

//        protected void DtStart_TextChanged(object sender, EventArgs e)
//        {
//            int? CS$0$0000;
//            if (this.IncidentId.HasValue && (((CS$0$0000 = this.IncidentId).GetValueOrDefault() > 0) && CS$0$0000.HasValue))
//            {
//                this.SetValues();
//            }
//            if ((this.DtStart.Text != string.Empty) && (this.CmbDamageType.SelectedItem != null))
//            {
//                Damage damage = Enumerable.SingleOrDefault<Damage>(this.TlManager.GetDamagesForPropType(int.Parse(this.CmbPropType.SelectedValue)), (Func<Damage, bool>) (d => (d.ID == int.Parse(this.CmbDamageType.SelectedValue))));
//                MgExtensions.TrafficLights.DAL.Contract contract = this.TlManager.GetContract(this.CurrentJunctionId.To<int>(), new int?(this.CmbPropType.SelectedValue.To<int>()));
//                if (damage != null)
//                {
//                    short? defaultPriority = damage.Priority;
//                    int? defaultStdTime = (contract.ContractOwner == "עירייה") ? damage.StdTimeMuni : damage.StdTimeMetro;
//                    if (defaultStdTime.HasValue)
//                    {
//                        DateTime date;
//                        if (!DateTime.TryParse(this.DtStart.Text, new CultureInfo("he-IL"), DateTimeStyles.None, out date))
//                        {
//                            return;
//                        }
//                        this.DtDue.Value = date.AddMinutes((double) defaultStdTime.Value).ToString("dd/MM/yyyy HH:mm", new CultureInfo("he-IL").DateTimeFormat);
//                    }
//                    if (defaultPriority.HasValue)
//                    {
//                        this.CmbPriority.SelectedValue = defaultPriority.Value.ToString();
//                    }
//                }
//            }
//        }

//        private MgExtensions.TrafficLights.DAL.Incident GetIncident(int selectedIncidentId)
//        {
//            return this.TlManager.GetIncedentById(selectedIncidentId);
//        }

//        private Prop GetPropOrCreate(int currentJunctionId, int uid, int propTypeId)
//        {
//            Prop result = this.TlManager.GetPropByUID(uid);
//            if ((result == null) && (uid > 0))
//            {
//                result = this.TlManager.CreateProp(currentJunctionId, propTypeId, uid);
//            }
//            return result;
//        }

//        private Fix GetSelectedFix()
//        {
//            Fix selectedFix = null;
//            string fixParam = this.CmbFixType.UniqueID.Replace('$', '_') + "inputField";
//            if ((!string.IsNullOrEmpty(this.Page.Request[fixParam]) && !string.IsNullOrEmpty(this.Page.Request[fixParam].Trim())) && (this.CmbFixType.SelectedItem.Text != this.Page.Request[fixParam]))
//            {
//                return this.TlManager.AddNewFix(this.PropTypeId.Value, this.Page.Request[fixParam]);
//            }
//            if (this.CmbFixType.SelectedIndex > -1)
//            {
//                selectedFix = this.TlManager.GetFixById(int.Parse(this.CmbFixType.SelectedValue));
//            }
//            return selectedFix;
//        }

//        private void OnIncidendSaved()
//        {
//            this._refreshMap = true;
//        }

//        private void OnIncidentClosed()
//        {
//            if (this.IncidentClosed != null)
//            {
//                this.IncidentClosed(this, EventArgs.Empty);
//            }
//        }

//        protected void Page_Load(object sender, EventArgs e)
//        {
//        }

//        private void ResetFields()
//        {
//            this.tbxCallCenterID.Text = string.Empty;
//            this.txtDamageDescription.InnerText = string.Empty;
//            this.DtStart.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
//            this.DtDue.Value = string.Empty;
//            this.DtEnd.Value = string.Empty;
//            this.txtIncidentFactor.InnerText = string.Empty;
//            this.txtComment.InnerText = string.Empty;
//            this.CmbDamageType.SelectedIndex = -1;
//            this.CmbPriority.SelectedValue = "1";
//            this.CmbStatus.SelectedValue = "7";
//            this.CmbReporterType.SelectedValue = "1";
//            this.CmbFixType.SelectedIndex = -1;
//        }

//        private static void SetIncidentProperties(MgExtensions.TrafficLights.DAL.Incident incidentItem, int selectedDamageTypeId, string callCenterIdStr, string description, int selectedReporterId, string reporterName, string reporterPhone, string reporterEmail, int selectedContractId, int selectedPriorityId, int selectedStatusId, string incidentFactorStr, Fix selectedFix, string notes)
//        {
//            incidentItem.DamageID = new int?(selectedDamageTypeId);
//            incidentItem.CallCenterID = callCenterIdStr;
//            incidentItem.Description = description;
//            incidentItem.ContractID = new int?(selectedContractId);
//            incidentItem.PriorityID = new int?(selectedPriorityId);
//            incidentItem.IncidentFactor = incidentFactorStr;
//            incidentItem.StatusID = new int?(selectedStatusId);
//            incidentItem.ReporterID = new int?(selectedReporterId);
//            incidentItem.ReporterName = reporterName;
//            incidentItem.ReporterPhone = reporterPhone;
//            incidentItem.ReporterEmail = reporterEmail;
//            incidentItem.Fix = selectedFix;
//            incidentItem.Notes = notes;
//            incidentItem.StatusDate = new DateTime?(DateTime.Now);
//        }

//        public void SetValues()
//        {
//            if (this.CurrentIncident != null)
//            {
//                this.txtPropUID.Value = (this.CurrentIncident.Prop != null) ? this.CurrentIncident.Prop.UID.ToString() : this.UID.ToString();
//                if (this.CurrentIncident.DamageID.HasValue)
//                {
//                    this.CmbDamageType.SelectedValue = this.CurrentIncident.DamageID.ToString();
//                }
//                if (this.CurrentIncident.PriorityID.HasValue)
//                {
//                    this.CmbPriority.SelectedValue = this.CurrentIncident.PriorityID.ToString();
//                }
//                if (this.CurrentIncident.StatusID.HasValue)
//                {
//                    this.CmbStatus.SelectedValue = this.CurrentIncident.StatusID.ToString();
//                }
//                if (this.CurrentIncident.ReporterID.HasValue)
//                {
//                    this.CmbReporterType.SelectedValue = this.CurrentIncident.ReporterID.ToString();
//                }
//                if (this.CurrentIncident.FixID.HasValue)
//                {
//                    this.CmbFixType.SelectedValue = this.CurrentIncident.FixID.ToString();
//                }
//                this.tbxCallCenterID.Text = this.CurrentIncident.CallCenterID;
//                this.txtDamageDescription.InnerText = this.CurrentIncident.Description;
//                this.DtStart.Text = this.CurrentIncident.DtStart.HasValue ? this.CurrentIncident.DtStart.Value.ToString("dd/MM/yyyy HH:mm") : string.Empty;
//                this.DtDue.Value = this.CurrentIncident.DtDue.HasValue ? this.CurrentIncident.DtDue.Value.ToString("dd/MM/yyyy HH:mm") : string.Empty;
//                this.DtEnd.Value = this.CurrentIncident.DtEnd.HasValue ? this.CurrentIncident.DtEnd.Value.ToString("dd/MM/yyyy HH:mm") : string.Empty;
//                this.txtIncidentFactor.InnerText = this.CurrentIncident.IncidentFactor;
//                this.txtComment.InnerText = this.CurrentIncident.Notes;
//                this.txtReporterName.Text = this.CurrentIncident.ReporterName;
//                this.txtReporterPhone.Text = this.CurrentIncident.ReporterPhone;
//                this.txtReporterEmail.Text = this.CurrentIncident.ReporterEmail;
//                this.txtLoggedBy.Text = (this.CurrentIncident.User != null) ? this.CurrentIncident.User.Title : string.Empty;
//            }
//        }

//        private MgExtensions.TrafficLights.DAL.Contract Contract { get; set; }

//        private Junction CurrentJunction { get; set; }

//        public int? CurrentJunctionId
//        {
//            get
//            {
//                if (this.ViewState["JunctionID"] == null)
//                {
//                    return null;
//                }
//                return new int?(int.Parse(this.ViewState["JunctionID"].ToString()));
//            }
//            set
//            {
//                this.ViewState["JunctionID"] = value;
//            }
//        }

//        private Prop CurrentProp { get; set; }

//        public int? IncidentId
//        {
//            get
//            {
//                if (this.ViewState["IncidentID"] == null)
//                {
//                    return null;
//                }
//                return new int?(int.Parse(this.ViewState["IncidentID"].ToString()));
//            }
//            set
//            {
//                this.ViewState["IncidentID"] = value;
//            }
//        }

//        public int? PropTypeId
//        {
//            get
//            {
//                if (this.ViewState["PropTypeID"] == null)
//                {
//                    return null;
//                }
//                return new int?(int.Parse(this.ViewState["PropTypeID"].ToString()));
//            }
//            set
//            {
//                this.ViewState["PropTypeID"] = value;
//            }
//        }

//        public TrafficLightsManager TlManager { get; set; }

//        public int UID
//        {
//            get
//            {
//                return this._uid;
//            }
//            set
//            {
//                this._uid = value;
//            }
//        }
//    }
//}

